/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.free.ts.contact.web;

import be.free.ts.support.web.AsyncBasicPresenter;
import com.github.gwtbootstrap.client.ui.ButtonCell;
import com.github.gwtbootstrap.client.ui.CellTable;
import com.github.gwtbootstrap.client.ui.SimplePager;
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
import com.github.gwtbootstrap.client.ui.resources.ButtonSize;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.inject.client.AsyncProvider;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.web.bindery.requestfactory.shared.Receiver;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;

import java.util.Date;

/**
 * @author Christophe De Blende
 * @since 05/01/13
 */
public class ListContactsPresenter extends AsyncBasicPresenter<ListContactsPresenter.Display> implements ClickHandler, FieldUpdater<ContactProxy, String> {

	private final ContactPresenter contactPresenter;

	private final Provider<ContactRequest> contactRequest;

	private AsyncDataProvider<ContactProxy> provider;

	@Inject
	public ListContactsPresenter(AsyncProvider<Display> display, EventBus eventBus, ContactPresenter contactPresenter,
								 Provider<ContactRequest> contactRequest1) {
		super(display, eventBus);
		this.contactPresenter = contactPresenter;
		this.contactRequest = contactRequest1;
	}

	@Override
	protected void onBind() {
		bindContactPresenter();
		bindSearchClickHandler();
		bindContactTableAndPager();
	}

	private void bindContactTableAndPager() {
		CellTable<ContactProxy> table = display.getCellTabe();
		bindPagerWithContactTable(table);
		bindDeleteColumnWithContactTable(table);
		bindEditColumnWithContactTable(table);
		bindSsinColumnWithContactTable(table);
		bindFirstNameColumnWithContactTable(table);
		bindLastNameColumnWithContactTable(table);
		bindBirthDateColumnWithContactTable(table);
	}

	private void bindPagerWithContactTable(CellTable<ContactProxy> table) {
		SimplePager pager = display.getPager();
		pager.setDisplay(table);

		table.setPageSize(10);
	}

	private void bindBirthDateColumnWithContactTable(CellTable<ContactProxy> table) {
		table.addColumn(new Column<ContactProxy, Date>(new DateCell()) {
			@Override
			public Date getValue(ContactProxy object) {
				return object.getBirthDate();
			}
		}, "Birth Date");
	}

	private void bindLastNameColumnWithContactTable(CellTable<ContactProxy> table) {
		table.addColumn(new TextColumn<ContactProxy>() {
			@Override
			public String getValue(ContactProxy object) {
				return object.getLastName();
			}
		}, "Last Name");
	}

	private void bindFirstNameColumnWithContactTable(CellTable<ContactProxy> table) {
		table.addColumn(new TextColumn<ContactProxy>() {
			@Override
			public String getValue(ContactProxy object) {
				return object.getFirstName();
			}
		}, "First Name");
	}

	private void bindSsinColumnWithContactTable(CellTable<ContactProxy> table) {
		table.addColumn(new TextColumn<ContactProxy>() {
			@Override
			public String getValue(ContactProxy object) {
				return object.getSsin();
			}
		}, "Ssin");
	}

	private void bindEditColumnWithContactTable(CellTable<ContactProxy> table) {
		Column<ContactProxy, String> editColumn = new Column<ContactProxy, String>(new ButtonCell(ButtonType.DEFAULT, ButtonSize.MINI)) {
			@Override
			public String getValue(ContactProxy c) {
				return "Edit";
			}
		};
		editColumn.setFieldUpdater(this);
		table.addColumn(editColumn, "Edit");
	}

	private void bindDeleteColumnWithContactTable(CellTable<ContactProxy> table) {
		Column<ContactProxy, String> deleteColumn = new Column<ContactProxy, String>(new ButtonCell(ButtonType.DANGER, ButtonSize.MINI)) {
			@Override
			public String getValue(ContactProxy c) {
				return "Delete";
			}
		};
		deleteColumn.setFieldUpdater(this);
		table.addColumn(deleteColumn, "Delete");
	}

	private void bindContactPresenter() {
		if (!contactPresenter.isBound()) {
			contactPresenter.bind();
			contactPresenter.revealDisplay();
		}
	}

	private void bindSearchClickHandler() {
		display.getSearch().addClickHandler(this);
	}

	@Override
	protected void onUnbind() {

	}

	@Override
	protected void onRevealDisplay() {
		provider = new AllContactsProvider(contactRequest);
		provider.addDataDisplay(display.getCellTabe());

		contactPresenter.getDisplay().getContent().clear();
		contactPresenter.getDisplay().getContent().add(display.asWidget());
	}

	@Override
	public void onClick(ClickEvent event) {
		display.getPager().firstPage();
		provider.removeDataDisplay(display.getCellTabe());

		provider = new FilteredContactsProvider(contactRequest, display);
		provider.addDataDisplay(display.getCellTabe());

	}

	@Override
	public void update(int index, ContactProxy object, String value) {
		contactRequest.get().delete(object).fire(new Receiver<Void>() {
			@Override
			public void onSuccess(Void response) {
				display.getCellTabe().setVisibleRangeAndClearData(display.getCellTabe().getVisibleRange(), true);
			}
		});
	}

	public interface Display extends WidgetDisplay {

		CellTable<ContactProxy> getCellTabe();

		SimplePager getPager();

		String getNiss();

		String getFirstName();

		String getLastName();

		HasClickHandlers getSearch();

	}

}